package com.wd.http;

import java.io.InputStream;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wd.util.InputStreamUtil;

/**
 * 
 * @author Nassir Wen
 * 
 */
public class HttpRequest {

	private static Log log = LogFactory.getLog(HttpRequest.class);

	private static final String userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";

	private static HttpClient hc = null;

	public HttpRequest() {
		init();
	}

	private void init() {
		hc = new HttpClient();
		hc.getParams().setConnectionManagerTimeout(60 * 1000);
		hc.getParams().setSoTimeout(60 * 1000);
		hc.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler(1, false));
	}

	private void post() {
		// String url = "http://www.sxcoal.com/user/login.aspx";
		// PostMethod postMethod = null;
		// postMethod = new PostMethod(url);
		// postMethod.getParams().setCookiePolicy(
		// CookiePolicy.BROWSER_COMPATIBILITY);
		// postMethod.setRequestHeader("User-Agent", userAgent);
		// postMethod.setRequestHeader("Referer", "http://www.sxcoal.com");
		// NameValuePair[] data = { new NameValuePair("password",
		// Configuration.getInstance().getUser()),
		// new NameValuePair("username", Configuration.getInstance().getPass())
		// };
		// postMethod.setRequestBody(data);
		// // String temp = "";
		// try {
		// int statusCode = hc.executeMethod(postMethod);
		// Cookie[] cookies = hc.getState().getCookies();
		// log.info("POST statusCode -->" +statusCode);
		// if (cookies != null) {
		// for (int i = 0; i < cookies.length; i++) {
		// cookie += cookies[i].toString() + ";";
		// }
		// cookie = cookie.substring(0, cookie.length() - 1);
		// }
		// } catch (HttpException e) {
		// e.printStackTrace();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }finally{
		// realeaseHttpMethod(postMethod);
		// }
	}

	public String getRequest(String theUrl) {
		HttpMethod method = null;
		String content = null;
		try {
			method = new GetMethod(theUrl);
			method.getParams().setCookiePolicy(
					CookiePolicy.BROWSER_COMPATIBILITY);
			method.setRequestHeader("User-Agent", userAgent);
			int statusCode = hc.executeMethod(method);
			log.info("GET statusCode -->" + statusCode);
			InputStream inputStrean = method.getResponseBodyAsStream();
			byte [] page = InputStreamUtil.inputStream2BytesArray(inputStrean);
			if(page == null){
				return content;
			}
			content = new String(page, "UTF-8");
			return content;
		} catch (Exception e) {
			log.error(e);
		} finally {
			realeaseHttpMethod(method);
		}
		return content;
	}

	private void realeaseHttpMethod(HttpMethod method) {
		if (method != null) {
			method.releaseConnection();
		}
	}
}
